<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Customer</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" auth="notice:add">添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="export" auth="notice:export">导出</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="batchDel" auth="notice:delete">删除</button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit" auth="notice:delete">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" auth="notice:delete">删除</a>
        </script>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script type="text/html" id="statusSwitch">
    <input type="checkbox" value="{{d.id}}"
           {{d.status==1 ? 'checked' : ''}} lay-skin="switch" lay-filter="switchStatus" lay-text="ON|OFF"/>
</script>
<script type="text/html" id="preShow">
    <button class="layui-btn layui-btn-xs layui-btn-success" pre-show data-id="{{d.id}}">预览</button>
</script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var noticeTypes = getEnum('noticeType')
        table.render({
            elem: '#currentTableId',
            url: '/admin/noticeJs/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'print' ],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'title', title: '标题'},
                {field: 'content', title: '内容'},
                {field: 'status',  title: '状态',templet:'#statusSwitch'},
                {field: 'preShow',  title: '预览',templet:"#preShow"},
                {field: 'sorts',  title: '排序号'},
                {field: 'type',  title: '类型',templet:function (row) {
                    return noticeTypes[row.type]
                }},
                {field: 'created_at', title: '创建时间'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');
            return false;
        });

        $('body').on('click','[pre-show]',function () {
            var id = $(this).attr('data-id');
            parent.window.open(localStorage.getItem('web_url')+'noticeJs/'+id+"?preshow=1")
        })

        function dels(ids,callback){ //通用删除方法
           layer.confirm('确定要删除吗?',{icon:3},function (index) {
                $.post('/admin/noticeJs/delete',{_method: 'delete',ids:ids},callback)
               layer.close(index)
            });
        }

        /**
         * toolbar监听事件 顶部
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/notice/formJs.html',
                });

            } else if (obj.event === 'batchDel'){
                var checkStatus = table.checkStatus('currentTableId')
                var data = checkStatus.data;
                var ids = []
                for(var k in data){
                    ids.push(data[k].id)
                }
                if (ids.length==0) return false;
                dels(ids,function(res){
                    if (res.code === 0){
                        layer.msg('删除成功',{icon:1,time:500},function(){
                            table.reload('currentTableId');
                        })
                    } else {
                        layer.msg(res.msg,{icon:2})
                    }
                });
            }
            else if(obj.event==='export'){
                var searchData = form.val('table-search');
                asyncDownload(
                    '/admin/noticeJs/export',
                    {page:1,limit:99999,searchParams:searchData},
                    {token:localStorage.getItem('adminToken')}
                );
            }
        });

        //右侧
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/notice/formJs.html?id='+data.id,
                });

                return false;
            } else if (obj.event === 'delete') {
                dels([data.id],function(res){
                    if (res.code ===0){
                        layer.msg('删除成功',{icon:1,time:500},function(){
                            obj.del();
                        })
                    }else {
                        layer.msg(res.msg,{icon:2})
                    }
                })
            }
        });
        form.on('switch(switchStatus)',function (data) {
            var id = data.value
            var checked = $(data.elem).prop('checked');
            var status = checked?1:0;
            $.post('/admin/noticeJs/'+id,{_method:'put',status:status},function(res){
                if (res.code === 0){
                    layer.msg('success',{icon:1})
                }
            })
        })
    });
</script>
</body>
</html>